<?php
namespace backend\controllers;
use app\models\VrSapce;
use backend\models\SysItem;
use backend\models\TaozhuangVrPos;
use Yii;
use yii\helpers\Json;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;

use yii\web\UploadedFile;
use backend\models\UploadForm;
use PHPThumb\GD;
use backend\models\Image;

use yii\web\ForbiddenHttpException;


use liangzoe\mobilesmser\TextSmser;
use dektrium\user\models\query\all;
use yii\data\Pagination;
use yii\data\ArrayDataProvider;
use backend\models\Vr;
use backend\models\VrSearch;
use backend\models\AdminInfo;
use \backend\models\VrPos;
use backend\models\Taozhuang;
use backend\models\TaozhuangVrExch;
use backend\models\TaozhuangVrBuffer;
/**
 * SysItemController implements the CRUD actions for SysItem model.
 */
class VrController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    public function beforeAction($action)
    {
    	//如果未登录，则直接跳转
    	if(Yii::$app->user->isGuest){
    		return $this->redirect(['/site/login']);
    	}
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	if(\Yii::$app->user->can('vr-manage') ){
    	   return true;
    		 
    	}else{
    		throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
    	}
    }
    //允许访问
    function _access($action){
    	$action_list = array(
    		'index',
    		'upload',
    	    'view',
    	    'create',
    	    'update',
    	    'delete',
            'add-item',
            'delete-item'
    	);
    	if(in_array($action, $action_list)){
    		return true;
    	}
    }
     
    public function actionIndex()
    {
        $searchModel = new VrSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
  
    /**
     * Displays a single SysItem model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
    	$this->layout = 'main_small';
    	$model = $this->findModel($id);
        return $this->render('view', [
            'model' => $model,
        ]);
    }
    
    /**
     * Creates a new SysItem model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Vr();	
        $model->app_return=0;
        $model->vr_flag=0;
        if ($model->load(Yii::$app->request->post())) {
            if($model->validate()){
                $model->image_upload = UploadedFile::getInstance($model, 'image_upload');
                if ($model->image_upload) {
                    $image_arr = Image::save_image_to_files($model, '414', $model->id.'_'.$model->name, 'item_default_image_thumb', 'item_default_image_thumb');
                    $model->vr_img = $image_arr['img_url_id'];
                    $model->image_upload = '';
                }


                $model->save();
                $new_model=Vr::findOne($model->id);
                $new_model->vr_url=strval($model->id);
                $result=$new_model->save();
                return $this->redirect(['view', 'id' => $model->id]);
           }else{
               echo '<script type="text/javascript">alert("验证未通过");</script>';
               return $this->redirect(['create']);
           }
       } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
    /**
     * Updates an existing SysItem model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
    	
    	$this->layout = 'main_small';
        $model = $this->findModel($id);

	        if ($model->load(Yii::$app->request->post())&&$model->validate()) { 
	            $model->image_upload = UploadedFile::getInstance($model,'image_upload');
	            if ($model->image_upload) {
	                $image_arr = Image::save_image_to_files($model, '414', $model->id.'_'.$model->name, 'item_default_image_thumb', 'item_default_image_thumb');
	                $model->vr_img=$image_arr["img_url_id"];
	                $model->image_upload = '';
	            }
	        	$model->save();
	            return $this->redirect(['view', 'id' => $model->id]);
	        } else {
	            return $this->render('update', [
	                'model' => $model,
	          		'allSpace'=>VrPos::getPosForVr($id)
	            ]);
	        }

    }

    /**
     * Deletes an existing SysItem model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }

    /**
     * vr空间删除单品
     * @author liuwenxia
     * @param $itemId
     * @param $posId
     * @return string
     */
    public function actionDeleteItem($itemId, $posId){
        $ret = ['error'=>0, 'msg'=>''];
        if(VrPos::deleteItem($itemId, $posId)){
            $ret['msg'] = '删除单品成功';;
        }else{
            $ret['error'] = 2;
            $ret['msg'] = '删除单品失败';
        };
        return Json::encode($ret);
    }
   
    /**
     * vr空间添加单品
     * @author liuwenxia
     * @param 单品id $itemId
     * @param vr_pos的id  $vrId
     * @param 空间id  $spaceId
     * @return string
     */
    public function actionAddItem($itemId, $vrId, $spaceId){
        $ret = ['error'=>0, 'msg'=>''];
        $item = SysItem::findOne($itemId);

        if($item == null){
            $ret['error'] = 1;
            $ret['msg'] = '单品不存在';
        }elseif(VrPos::addItem($itemId, $vrId, $spaceId)){
            $ret['msg'] = '添加单品成功';
        }else{
            $ret['error'] = 2;
            $ret['msg'] = '添加单品失败';
        }
        return  Json::encode($ret);
    }
    /**
     * Finds the SysItem model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return SysItem the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        $model = Vr::findOne($id);
        if($model !== null){
            return $model;
        }else{
        	throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    /** 用于ueditor插件
     * (non-PHPdoc)
     * @see \yii\base\Controller::actions()
     * by suiyue
     */
    public function actions(){
    	$actions = parent::actions();
    	$actions['upload'] = [
    					'class'=>'backend\widgets\ueditor\UEditorAction',
    					'config'=>[
    							'imageUrlPrefix'=>"https://".$_SERVER['SERVER_NAME']."/gjj/backend/web/images/",//图片访问路径前缀
			   					'imagePathFormat'=>'{rand:2}/{time}{rand:6}',
			   				    'imageRoot'=>\Yii::getAlias("images")
    					]
    			];
    	return $actions;
    }
    /**
     *
     *  套装列表勾选vr页面
     *   @author liuwenxia
     */
    public function actionTzIndex(){
        //查询所有套装
        $tzlist=Taozhuang::find()->select("sid,name")->asArray()->orderBy("top_time desc,sid asc")->all();
        return $this->render('tz_list', [
            'tzlist' => $tzlist,
        ]);
    }

    /**
     * 查询当前套装的vr空间单品列表
     *  @author liuwenxia
     * @param 套装id  $tzid
     * @return string
     */
    public function actionVrEdit($tzid,$vr_pos_id){
     
        //计算当前套装vr图片列表
        //查询所有套装
        $sql="SELECT taozhuang_vr_pos.id,taozhuang_vr_pos.tzid,taozhuang_vr_pos.pos,sys_image.id img_id,sys_image.`name` img_name,sys_image.url img_url,sys_image.width,sys_image.height  FROM taozhuang_vr_pos
            LEFT JOIN sys_image ON sys_image.id=taozhuang_vr_pos.`img_id`
             WHERE tzid=:tzid AND  `order`=0   order by taozhuang_vr_pos.pos asc ";
        //查询所有vr_exch
        $vr_exch=TaozhuangVrExch::find()->where("tzid=:tzid",array(":tzid"=>$tzid))->orderBy("pos asc ")->asArray()->all();
        $vr_pos_main=TaozhuangVrPos::findBySql($sql,array(":tzid"=>$tzid))->asArray()->all();
        //查询所有切割 vr_buffer
        $vr_buffer=TaozhuangVrBuffer::find()->where("tzid=:tzid",array(":tzid"=>$tzid))->asArray()->all();
        $vr_pos=0;
        if(intval($vr_pos_id)>0){
            $vr_pos_info=TaozhuangVrPos::findOne($vr_pos_id);
            if($vr_pos_info){
                $vr_pos=$vr_pos_info->pos;
            }
        } 
        return $this->render('vr_edit', [
            'tid' => $tzid,
            'vr_pos_id'=>$vr_pos_id,
            'vr_pos'=>$vr_pos,
            'vr_pos_main'=>$vr_pos_main,
            'vr_exch'=>$vr_exch,
            'vr_buffer'=>$vr_buffer

        ]);
    }
    /**
     * 根据vr_pos的id,查询所有同层的单品图 
     * @author liuwenxia
     * @param $pos获取当前vr当前pos的图片
     */
    public function actionGetOtherPos($tzid,$pos){
        $sql=" SELECT taozhuang_vr_pos.id,taozhuang_vr_pos.item_id,taozhuang_vr_pos.`order`,taozhuang_vr_pos.pos,sys_image.id img_id,sys_image.`name` img_name,sys_image.url img_url FROM taozhuang_vr_pos
                LEFT JOIN sys_image ON sys_image.id=taozhuang_vr_pos.`img_id`
                 WHERE tzid=:tzid AND  pos=:pos   order by taozhuang_vr_pos.order asc";
        $vr_pos_list=TaozhuangVrPos::findBySql($sql,array(":tzid"=>$tzid,":pos"=>$pos))->asArray()->all();
     
        for($i=0;$i<count($vr_pos_list);$i++){
            $vr_pos_list[$i]["img_url"]="". $vr_pos_list[$i]["img_url"];
        }
        echo json_encode($vr_pos_list);exit();
    }

    /**
     *  获取当前套装vr的pos
     *  @author liuwenxia
     *  @param tzid 套装id
     */
    public function actionGetMainPos($tzid){
        $sql="SELECT taozhuang_vr_pos.id,taozhuang_vr_pos.tzid,taozhuang_vr_pos.pos,sys_image.id img_id,sys_image.`name` img_name  FROM taozhuang_vr_pos
            LEFT JOIN sys_image ON sys_image.id=taozhuang_vr_pos.`img_id`
             WHERE tzid=:tzid AND  `order`=0   order by taozhuang_vr_pos.pos asc ";
        $vr_pos_main=TaozhuangVrPos::findBySql($sql,array(":tzid"=>$tzid))->asArray()->all();
        echo json_encode($vr_pos_main);exit(); 
    }

    /**
     * 获取当前套装vr的pos
     * @author liuwenxia
     * 删除vr_pos数据
     * @param $pos_id删除的id
     */
    public function actionDelVrPos($pos_id){
        $pos_id=intval($pos_id);
        if($pos_id==0){
            echo 2;exit();
        }else{
            $vr_pos=TaozhuangVrPos::findOne($pos_id);
            if($vr_pos){
                $order=intval($vr_pos->order);
                if($order==0){
                    $pos=$vr_pos->pos;
                    $tzid=$vr_pos->tzid;
                    $result=TaozhuangVrPos::deleteAll("tzid=:tzid and pos=:pos",array(":tzid"=>$tzid,":pos"=>$pos));
                    //重新排序 
                    $sql="update  taozhuang_vr_pos  SET pos=pos-1 WHERE tzid=:tzid  AND pos>:pos";
                    $result=Yii::$app->db->createCommand($sql,array(":tzid"=>$tzid,":pos"=>$pos))->execute();
                    echo $result;exit();
                }else{
                    $pos=$vr_pos->pos;
                    $tzid=$vr_pos->tzid;
                    $order=$vr_pos->order;
                    $result=$vr_pos->delete();
                    //重新排序
                    $sql="update  taozhuang_vr_pos  SET `order`=`order`-1  WHERE tzid=:tzid  AND pos=:pos and `order`>:order";
                    $result=Yii::$app->db->createCommand($sql,array(":tzid"=>$tzid,":pos"=>$pos,":order"=>$order))->execute();
                    echo $result;exit();
                
                }
            }else{
                echo 2;exit();
            }
        }

    }
    /**
     * 
     * 设置为默认 
     * @author liuwenxia
     * @param $pos_id 
     */
    public function actionSetVrPosDefault($pos_id){
        //查询当前pos的order  
        $vr_pos=TaozhuangVrPos::findOne($pos_id);
        if($vr_pos){
            $order=intval($vr_pos->order);
            if($order!=0){
                $pos=$vr_pos->pos;
                $tzid=$vr_pos->tzid;
                $vr_old_default=TaozhuangVrPos::find()->where("tzid=:tzid and pos=:pos and `order`=0",array(":tzid"=>$tzid,":pos"=>$pos))->one();
                if($vr_old_default){
                    //先设置当前pos为-1 
                    $vr_pos->order=-1;
                    $result=$vr_pos->save();
                    //设置默认的pos的排序为当前pos的排序
                    $vr_old_default->order=$order;
                    $result=$vr_old_default->save();
                    $vr_pos->order=0;
                    $result=$vr_pos->save();
                    echo $result;exit();
                }else{
                    $vr_pos->order=0;
                    $result=$vr_pos->save();
                    echo $result;exit();
                }
            }
        }else{
            echo 2;exit();
        }
        
    }

    /**
     * 修改单品id
     * @author liuwenxia
     * @param $pos_id
     * @param $item_id修改vrpos的item_id
     */

    public function actionUpPosItemid($pos_id,$item_id){
        $vr_pos=TaozhuangVrPos::findOne($pos_id);
        if($vr_pos&&intval($item_id)>0){
            $vr_pos->item_id=intval($item_id);
            $result=$vr_pos->save();
            echo $result;exit();
        }else{
            echo 2;exit();
        }
    }

    /**
     * 操作完图片回调vr空间列表页
     * @author liuwenxia
     * @param $vrposid操作了vr图片修改，跳转到vr/vr-edit页面
     */
    public function actionVrImgOp($tzid,$vr_pos_id){
        if(intval($tzid)>0){
            $this->redirect(['vr/vr-edit','tzid'=>$tzid,'vr_pos_id'=>$vr_pos_id]);
        }
    }

    /**
     *  vr_pos 选择了图层 添加下面的可选择图片
     * @author liuwenxia
     * @param $img_id
     * @param $vr_id
     */
    public function  actionUpdateImageOther($id,$vr_id){
        if(intval($id)>0&&intval($vr_id)>0){
            $vr_pos=TaozhuangVrPos::findOne($vr_id);
            $tzid=$vr_pos->tzid;
            $pos=$vr_pos->pos;
            $vr_last_pos=TaozhuangVrPos::find()->where("tzid=:tzid  and pos=:pos",array(":tzid"=>$tzid,":pos"=>$pos))->orderBy("`order` desc")->one();
            $last_order=intval($vr_last_pos->order);

            $vr_pos_new=new TaozhuangVrPos();
            $vr_pos_new->tzid=$tzid;
            $vr_pos_new->pos=$pos;
            $vr_pos_new->order=$last_order+1;
            $vr_pos_new->img_id=$id;
            $vr_pos_new->item_id=0;
            $result=$vr_pos_new->save();
            echo $result;exit();
        }else{
            echo 2;exit();
        }
    }
    /**
     * 添加主图
     * @author liuwenxia
     * @param $id $tzid
     */
    public function  actionUpdateImageMain($id,$tzid){
        if(intval($id)==0||intval($tzid)==0){
            echo 2;exit();
        }
        $max_vr_pos=TaozhuangVrPos::find()->where("tzid=:tzid and `order`=0",array(":tzid"=>$tzid))->orderBy("pos desc")->one();
        if($max_vr_pos){
            $pos=intval($max_vr_pos->pos)+1;
            $vr_pos=new TaozhuangVrPos();
            $vr_pos->tzid=$tzid;
            $vr_pos->img_id=$id;
            $vr_pos->order=0;
            $vr_pos->pos=$pos;
            $vr_pos->item_id=0;
            $result=$vr_pos->save();
            echo $result;exit();
        }else{
            //如果不存在，直接新增
            $pos=1;
            $vr_pos=new TaozhuangVrPos();
            $vr_pos->tzid=$tzid;
            $vr_pos->img_id=$id;
            $vr_pos->order=0;
            $vr_pos->pos=$pos;
            $vr_pos->item_id=0;
            $result=$vr_pos->save();
            echo $result;exit();
        }
    }
    /**
     * vr 图片替换
     * @author liuwenxia
     */
    public function actionReplaceImageMain($id,$vr_id){
        if(intval($id)==0||intval($vr_id)==0){
            echo 2;exit();
        }
        $vr_pos=TaozhuangVrPos::findOne($vr_id);
        if($vr_pos){
            $vr_pos->img_id=$id;
            $result=$vr_pos->save();
            echo $result;exit();
        }else{
           echo 2;exit(); 
        }
    }
    /**
     * 插入image
     * @author liuwenxia
     */
    public function actionInsertImageMain($id,$vr_id){
        //获取要插入的vr位置
        if(intval($id)==0||intval($vr_id)==0){
            echo 2;exit();
        }else{
            $vr_pos=TaozhuangVrPos::findOne($vr_id);
            $pos=intval($vr_pos->pos);
            $tzid=$vr_pos->tzid;
            //修改>=pos的数据，pos+1
            $sql="update taozhuang_vr_pos set pos=pos+1 where tzid=$tzid   AND pos>=$pos";
            $result=yii::$app->db->createCommand($sql)->execute();
            if($result){
                //添加一条数据 pos=pos
                $new_vrpos=new TaozhuangVrPos();
                $new_vrpos->tzid=$tzid;
                $new_vrpos->img_id=$id;
                $new_vrpos->pos=$pos;
                $new_vrpos->order=0;
                $new_vrpos->item_id=0;
                $result=$new_vrpos->save();
            }
            
            echo $result;exit();
            
        }
        
    }
    /**
     * 获取vr的切换点 exch
     *  @author liuwenxia
     *  @param $tzid 套装id
     */
    public function actionGetVrexch($tzid){
        //查询所有vr_exch
        $vr_exch=TaozhuangVrExch::find()->where("tzid=:tzid",array(":tzid"=>$tzid))->orderBy("pos asc ")->asArray()->all();
       echo json_encode($vr_exch);exit();
    }
    /**
     * 添加切换点
     * @param   $main_pos_id
     *  @author liuwenxia
     */
    public function actionAddExch($main_pos_id){
        if(intval($main_pos_id)==0){
             echo 0;exit();
        }else{
            $vr_pos=TaozhuangVrPos::findOne($main_pos_id);
            if($vr_pos){
                $pos=intval($vr_pos->pos);
                $tzid=intval($vr_pos->tzid);
                $vr_exch=TaozhuangVrExch::find()->where("tzid=:tzid and pos=:pos",array(":tzid"=>$tzid,":pos"=>$pos))->one();
                if($vr_exch){
                    echo 2;exit();
                }else{
                    $vr_exch=new TaozhuangVrExch();
                    $vr_exch->tzid=$tzid;
                    $vr_exch->pos=$pos;
                    $vr_exch->x=50;
                    $vr_exch->y=50;
                    $vr_exch->goto_url='';
                    $vr_exch->goto_img='';
                    $result=$vr_exch->save();
                    echo $result;exit();
                }
            }else{
                echo 0;exit();
            }
            
        }
        
    }
    /**
     * 删除切换点
     * @param   $id
     *  @author liuwenxia
     */
    public function actionDelExch($id){
        
        $result=TaozhuangVrExch::deleteAll('id=:id',array(":id"=>$id));
        echo $result;exit();
    }
    /**
     * 添加vr跳转
     * @author liuwenxia
     */
    public function actionAddExchJump($tzid){
        $min_vr_exch=TaozhuangVrExch::find()->where("tzid=:tzid",array(":tzid"=>$tzid))->orderBy("pos asc")->one();
        if($min_vr_exch){
            $min_pos=intval($min_vr_exch->pos);
            if($min_pos>1){
                $pos=0;
            }else{
                $pos=intval($min_vr_exch->pos)-1;
            }
            
        }else{
            $pos=0;
        }
       
        
        $vr_exch=new TaozhuangVrExch();
        $vr_exch->tzid=$tzid;
        $vr_exch->pos=$pos;
        $vr_exch->x=50;
        $vr_exch->y=50;
        $vr_exch->goto_url='';
        $vr_exch->goto_img='';
        $result=$vr_exch->save();
        echo $result;exit();
    }
    /**
     * 修改跳转url
     * @param   $id
     * @param   $goto_url
     *  @author liuwenxia
     */
    public function actionUpExchJump($id,$goto_url){
        if(intval($id)==0){
            echo 0;exit();
        }else{
            $vr_exch=TaozhuangVrExch::findOne($id);
            $pos=intval($vr_exch->pos);
            if($pos>0){
                echo 2;exit();
            }else{
                $vr_exch->goto_url=$goto_url;
                $result=$vr_exch->save();
                echo $result;exit();
            }
        }
        
    }
    /**
     * 修改x坐标
     * @param   $id
     * @param   $x
     *  @author liuwenxia
     */
    public function actionUpExchX($id,$x){
        $vr_exch=TaozhuangVrExch::findOne($id);
        if($vr_exch){
            $vr_exch->x=$x;
            $result=$vr_exch->save();
            echo $result;exit();
        }else{
           echo 2;exit(); 
        }
        
    }
    /**
     * 修改y坐标
     * @param   $id
     * @param   $y
     *  @author liuwenxia
     */
    public function actionUpExchY($id,$y){
        $vr_exch=TaozhuangVrExch::findOne($id);
        if($vr_exch){
            $vr_exch->y=$y;
            $result=$vr_exch->save();
            echo $result;exit();
        }else{
            echo 2;exit();
        }
        
    }
    
    /**
     * 修改切换vr图标图片
     * @param   $goto_img
     * @param   $id
     *  @author liuwenxia
     */
    public function actionUpExchGotoimg($goto_img,$id){
        if(intval($id)==0||$goto_img==""){
            echo 0;exit();
        }else{
            $vr_exch=TaozhuangVrExch::findOne($id);
            $pos=intval($vr_exch->pos);
            
            if($vr_exch){
                if($pos>0){
                    echo 2;exit();
                }else{
                    $vr_exch->goto_img=$goto_img;
                    $result=$vr_exch->save();
                    echo $result;exit();
                }
            }else{
                echo 0;exit();
            }
            
        }
        
    }
    
    /**
     * 根据vr-pos的id  获取图片路径
     * @param   $id
     * @author liuwenxia
     */
    public function actionGetVrposImg($id){
        if(intval($id)==0){
            echo "";exit();
        }else{
            $sql=" SELECT sys_image.url img_url  FROM taozhuang_vr_pos 
                 LEFT JOIN sys_image ON taozhuang_vr_pos.`img_id`=sys_image.`id`
                 WHERE taozhuang_vr_pos.id=:id";
            $vr_pos=TaozhuangVrPos::findBySql($sql,array(":id"=>$id))->asArray()->one();
            if(count($vr_pos)>0){
               echo $vr_pos["img_url"];exit();
            }else{
                echo "";exit();
            }
        }
    }
    /**
     * 查询当前套装的每个图层默认图 查询当前套装所有热点xy坐标
     * @param 套装id $tzid
     * @author liuwenxia
     */
    public function actionRefreshImg($tzid){
        //查询所有套装
        $sql="SELECT taozhuang_vr_pos.id,taozhuang_vr_pos.tzid,taozhuang_vr_pos.pos,sys_image.id img_id,sys_image.`name` img_name,sys_image.url img_url,sys_image.width,sys_image.height  FROM taozhuang_vr_pos
            LEFT JOIN sys_image ON sys_image.id=taozhuang_vr_pos.`img_id`
             WHERE tzid=:tzid AND  `order`=0   order by taozhuang_vr_pos.pos asc ";
        //查询所有vr_exch
        $vr_exch=TaozhuangVrExch::find()->where("tzid=:tzid",array(":tzid"=>$tzid))->orderBy("pos asc ")->asArray()->all();
        $vr_pos_main=TaozhuangVrPos::findBySql($sql,array(":tzid"=>$tzid))->asArray()->all();
        $return_array=array("vr_pos_main"=>$vr_pos_main,"vr_exch"=>$vr_exch);
        echo json_encode($return_array);exit();
    }
    /**
     * 查看单条buffer数据
     * @param   $id
     */
    public function actionBufferImg($id){
        if(intval($id)>0){
            $sql=" SELECT   m_image.url m_url, b_image.url b_url, d_image.url d_url,f_image.url f_url,l_image.url l_url,r_image.url r_url,u_image.url u_url
                     FROM taozhuang_vr_buffer 
                     LEFT JOIN sys_image m_image ON m_image.id=taozhuang_vr_buffer.`img_id`
                     LEFT JOIN sys_image b_image ON b_image.id=taozhuang_vr_buffer.`img_b`
                     LEFT JOIN sys_image d_image ON d_image.id=taozhuang_vr_buffer.`img_d`
                     LEFT JOIN sys_image f_image ON f_image.id=taozhuang_vr_buffer.`img_f` 
                     LEFT JOIN sys_image l_image ON l_image.id=taozhuang_vr_buffer.`img_l` 
                     LEFT JOIN sys_image r_image ON r_image.id=taozhuang_vr_buffer.`img_r` 
                     LEFT JOIN sys_image u_image ON u_image.id=taozhuang_vr_buffer.`img_u` 
                     WHERE  taozhuang_vr_buffer.id=:id";
            $vr_buffer=TaozhuangVrBuffer::findBySql($sql,array(":id"=>$id))->asArray()->all();
            echo json_encode($vr_buffer,true);exit();
        }else{
            echo json_encode(array());exit();
        }
    }
    /**
     * 刷新Buffer数据
     * @param $tzid
     */
    public function actionRefreshBuffer($tzid){
        if(intval($tzid)>0){
            $vr_buffer=TaozhuangVrBuffer::find()->where("tzid=:tzid",array(":tzid"=>$tzid))->asArray()->all();
            echo json_encode($vr_buffer);exit();
        }else{
            echo json_encode(array());exit();
        }
    }

    /**
     * @param $tzid清除buffer数据
     */
    public function actionClearBuffer($tzid){
        if(intval($tzid)>0){
            $result=TaozhuangVrBuffer::deleteAll('tzid=:tzid',array(":tzid"=>$tzid));
            echo $result;exit();
        }else{
            echo 0;exit();
        }
    }

    /**
     * 重新生成Buffer
     */
    public function actionCreateBuffer($tzid){
        $sql="SELECT id FROM taozhuang_vr_buffer WHERE tzid=:tzid";
        $buffer_list=TaozhuangVrBuffer::findBySql($sql,array(":tzid"=>$tzid))->asArray()->all();
        $pos_array=array();
        if(intval($tzid)>0&&count($buffer_list)==0){
                $vr_pos_new=array();
                //所有图层
                $all_vr_pos=TaozhuangVrPos::get_all_pos($tzid);
                //查询到第一个多个的图层
                $first_multiple=TaozhuangVrPos::get_first_multiple($tzid);
                $multiple_desc=TaozhuangVrPos::get_multiple_desc($tzid);
                $multiple_pos=TaozhuangVrPos::get_multiple_id($tzid);
                $frist_multiple_pos=TaozhuangVrPos::get_first_multiple_id($tzid);
                $pos_find_array=TaozhuangVrPos::get_multiple_vrposid($tzid);
                if(count($multiple_desc)>0){
                    //for($i=0;$i<count($first_multiple);$i++){

                        //从其他图层 最后一个开始计算多个单品的图层
                        for($j=0;$j<count($multiple_desc);$j++){
                            $desc_pos=$multiple_desc[$j]["pos"];
                            $desc_count=$multiple_desc[$j]["count"];
                           // echo "<br/>当前图层:".$desc_pos ."<br/> ";
                            $ids=$multiple_desc[$j]["ids"];
                            if($j==0){//最后一个图层，直接前面所有默认的pos 追加当前pos的ids
                                //查询小于当前图层的所有默认图层
                                $small_pos_list_str=TaozhuangVrPos::get_small_pos($tzid,$desc_pos);
                                for($z=0;$z<count($ids);$z++){
                                     $z_pos_id=$ids[$z]["id"];
                                     $one_pos_str_list=TaozhuangVrPos::get_one_pos($tzid,$desc_pos);
                                     $pos_str=$small_pos_list_str.",".$z_pos_id.$one_pos_str_list;
                                     $pos_array[]=$pos_str;
                                   //  echo "<br/> pos_str1: ".$pos_str;
                                 }
                            }else{
                                //获取比当前图层更大的多图图层
                                $multiple_bigger=TaozhuangVrPos::get_multiple_bigger_desc($tzid,$desc_pos);
                                for($x=1;$x<count($ids);$x++){
                                    $x_pos_id=intval($ids[$x]["id"]);
                                    $x_pos_pos=$ids[$x]["pos"];
                                    //$x_pos_order=$ids[$x]["order"];
                                   //查询当前pos的当前id的前面默认id 当前id 后面默认id
                                    $order_pos_list_str=TaozhuangVrPos::get_order_pos_str($tzid,$x_pos_id);
                                  //  echo "<br/>当前pos_id:".$x_pos_id;
                                    for($y=0;$y<count($multiple_bigger);$y++){
                                        $y_pos=$multiple_bigger[$y]["pos"];
                                        $y_ids=$multiple_bigger[$y]["ids"];
                                        for($o=0;$o<count($y_ids);$o++){
                                            $o_pos_id=$y_ids[$o]["id"];
                                            $o_pos=intval($y_ids[$o]["pos"]);
                                            $o_order_order=intval($y_ids[$o]["order"]);
                                            if($o_pos==$multiple_pos){
                                                $between_pos_list_str=TaozhuangVrPos::get_between_pos_str($tzid,$x_pos_id,$o_pos_id);
                                                $one_pos_str_list=TaozhuangVrPos::get_one_pos($tzid,$o_pos);
                                                $pos_str=$order_pos_list_str.$x_pos_id.",".$between_pos_list_str.$o_pos_id.$one_pos_str_list;
                                                $pos_array[]=$pos_str;
                                          // echo "<br/>pos_str2:".$pos_str;
                                                
                                            }else if($x_pos_pos==$frist_multiple_pos){
                                               // echo "<br/>x_pos_pos==frist_multiple_pos<br/>";
                                                $more_multiple_bigger=TaozhuangVrPos::get_multiple_bigger_desc($tzid,$x_pos_pos);

                                                for($m=0;$m<count($more_multiple_bigger);$m++){
                                                    if($m==0){
                                                        continue;
                                                    }
                                                   $m_ids=$more_multiple_bigger[$m]["ids"];
                                                   $m_pos_pos=$more_multiple_bigger[$m]["pos"];
                                                   for($n=0;$n<count($m_ids);$n++){
                                                       $n_pos_id=$m_ids[$n]["id"];
                                                       $n_pos_pos=$m_ids[$n]["pos"];
                                                       $n_pos_order=intval($m_ids[$n]["order"]);
                                                        if($n_pos_pos==$multiple_pos){
                                                             $between_pos_list_str=TaozhuangVrPos::get_between_pos_str($tzid,$x_pos_id,$n_pos_id);
                                                             $one_pos_str_list=TaozhuangVrPos::get_one_pos($tzid,$n_pos_pos);
                                                             $pos_str=$x_pos_id.",".$between_pos_list_str.$n_pos_id.$one_pos_str_list;
                                                           $pos_array[]=$pos_str;
                                                           // echo "<br/> pos_str3:".$pos_str;
                                                       }else{
                                                            if($n_pos_order>0){
                                                               //查看后面多图
                                                              $more_multiple_bigger2=TaozhuangVrPos::get_multiple_bigger_desc($tzid,$n_pos_pos);
                                                              if(count($more_multiple_bigger2)==1){
                                                                  for($r=0;$r<count($more_multiple_bigger2[0]["ids"]);$r++){
                                                                      $pos_str=TaozhuangVrPos::get_pos_str_one($tzid,$more_multiple_bigger2[0]["ids"][$r]["id"],$more_multiple_bigger2[0]["ids"][$r]["pos"],$x_pos_id,$n_pos_id);
                                                                      $pos_array[]=$pos_str;
                                                                  //  echo "  pos_str4:".$pos_str."<br/>";
                                                                   }
                                                              }else{
                                                                  for($s=0;$s<count($more_multiple_bigger2);$s++){
                                                                      $s_ids=$more_multiple_bigger2[$s]["ids"];
                                                                      for($t=0;$t<count($s_ids);$t++){
                                                                          $s_pos_id=$s_ids[$t]["id"];
                                                                          $s_pos_pos=$s_ids[$t]["pos"];
                                                                          $s_pos_order=$s_ids[$t]["order"];
                                                                          if($s_pos_pos==$multiple_pos){
                                                                              $pos_str=TaozhuangVrPos::get_pos_str_two($tzid,$x_pos_id,$n_pos_id,$s_pos_id,$s_pos_pos);
                                                                              $pos_array[]=$pos_str;
                                                                             //  echo "<br/> pos_str5: ".$pos_str;
                                                                          }else{
                                                                              if($s_pos_order>0){
                                                                                  $between_pos_list_str=TaozhuangVrPos::get_between_pos_str($tzid,$x_pos_id,$n_pos_id);
                                                                                  $between_pos_list_str2=TaozhuangVrPos::get_between_pos_str($tzid,$n_pos_id,$s_pos_id);
                                                                                  //查询后面更大的
                                                                                  $more_multiple_bigger3=TaozhuangVrPos::get_multiple_bigger($tzid, $s_pos_pos);
                                                                                  for($u=0;$u<count($more_multiple_bigger3);$u++){
                                                                                      $u_ids=$more_multiple_bigger3[$u]["ids"];
                                                                                      for($v=0;$v<count($u_ids);$v++){
                                                                                          $pos_str=TaozhuangVrPos::get_pos_str_three($tzid, $x_pos_id, $n_pos_id, $s_pos_id, $u_ids[$v]["id"], $u_ids[$v]["pos"], $s_pos_id);
                                                                                          $pos_array[]=$pos_str;
                                                                                      //    echo "<br/>pos_str6: ".$pos_str;
                                                                                      }
                                                                                  }
                                                                                  
                                                                              }
                                                                          }
                                                                          
                                                                       
                                                                      }
                                                                  }
                                                                  
                                                              }
                                                           } 
                                                       }
                                                        
                                                   } 
                                                    
                                               }
                                               break;
                                            }else{

                                                    $more_multiple_bigger=TaozhuangVrPos::get_multiple_bigger_desc($tzid,$x_pos_pos);
                                                    if(in_array($x_pos_id, $pos_find_array)){
                                                        for($k=0;$k<count($more_multiple_bigger);$k++){
                                                            $k_ids=$more_multiple_bigger[$k]["ids"];

                                                            for($l=0;$l<count($k_ids);$l++){
                                                                $l_pos_id=$k_ids[$l]["id"];
                                                                $l_pos_pos=$k_ids[$l]["pos"];
                                                                $l_pos_order=$k_ids[$l]["order"];


                                                                /*  if($l_pos_order>0){ */
                                                                $l_more_multiple_bigger=TaozhuangVrPos::get_multiple_bigger_desc($tzid, $l_pos_pos);

                                                                if(count($l_more_multiple_bigger)>0){
                                                                    if($l_pos_pos!=$multiple_pos){


                                                                        for($q=0;$q<count($l_more_multiple_bigger);$q++){
                                                                            $q_ids=$l_more_multiple_bigger[$q]["ids"];
                                                                            for($a=0;$a<count($q_ids);$a++){
                                                                                $q_pos_id=$q_ids[$a]["id"];
                                                                                $q_pos_pos=$q_ids[$a]["pos"];
                                                                                $q_pos_order=$q_ids[$a]["order"];

                                                                                 if($l_pos_order>0){
                                                                                     if($q_pos_pos!=$multiple_pos){
                                                                                        if($q_pos_order>0){
                                                                                            //判断下层是否还有多个
                                                                                            $q_more_multiple_bigger=TaozhuangVrPos::get_multiple_bigger_desc($tzid,$q_pos_pos);
                                                                                            if(count($q_more_multiple_bigger)>0){
                                                                                                for($d=0;$d<count($q_more_multiple_bigger);$d++){
                                                                                                    $d_ids=$q_more_multiple_bigger[$d]["ids"];
                                                                                                    for($e=0;$e<count($d_ids);$e++){
                                                                                                        $e_pos_id=$d_ids[$e]["id"];
                                                                                                        $e_pos_pos=$d_ids[$e]["pos"];
                                                                                                        $e_pos_order=$d_ids[$e]["order"];
                                                                                                        //当前是最后一个 直接循环添加
                                                                                                        if($e_pos_pos==$multiple_pos){
                                                                                                         $pos_str=TaozhuangVrPos::get_pos_str_e($tzid,$order_pos_list_str,$q_pos_id,$x_pos_id,$l_pos_id,$e_pos_id,$e_pos_pos);
                                                                                                            $pos_array[]=$pos_str;
                                                                                                               // echo "<br/>pos_str7:".$pos_str;
                                                                                                        }else{
                                                                                                            if($e_pos_order>0){
                                                                                                                $e_more_multiple_bigger=TaozhuangVrPos::get_multiple_bigger_desc($tzid,$e_pos_pos);

                                                                                                                for($f=0;$f<count($e_more_multiple_bigger);$f++){
                                                                                                                    $f_ids=$e_more_multiple_bigger[$f]["ids"];
                                                                                                                    for($g=0;$g<count($f_ids);$g++){
                                                                                                                        $g_pos_id=$f_ids[$g]["id"];
                                                                                                                        $g_pos_pos=$f_ids[$g]["pos"];
                                                                                                                        $g_pos_order=$f_ids[$g]["order"];

                                                                                                                         $g_more_multiple_bigger=TaozhuangVrPos::get_multiple_bigger_desc($tzid,$g_pos_pos);


                                                                                                                         if(count($g_more_multiple_bigger)>0){
                                                                                                                             for($h=0;$h<count($g_more_multiple_bigger);$h++){
                                                                                                                                 $h_ids=$g_more_multiple_bigger[$h]["ids"];
                                                                                                                                 for($p=0;$p<count($h_ids);$p++){


                                                                                                                                     $p_pos_id=$h_ids[$p]["id"];$p_pos_pos=$h_ids[$p]["pos"];$p_pos_order=$h_ids[$p]["order"];

                                                                                                                                     if(($g_pos_pos!=$multiple_pos&&$g_pos_order== 0)||$g_pos_id==$multiple_pos){
                                                                                                                                        continue;
                                                                                                                                     }

                                                                                                                                     $pos_str=TaozhuangVrPos::get_pos_str_p($tzid,$order_pos_list_str,$x_pos_id,$l_pos_id,$q_pos_id,$e_pos_id,$g_pos_id,$p_pos_id,$p_pos_pos);
                                                                                                                                     $pos_array[]=$pos_str;
                                                                                                                                     /*  echo "<br/>test<br/>"; echo "<br/> order_pos_list_str: ".$order_pos_list_str;
                                                                                                                                       echo "  x_pos_id_str:".$x_pos_id_str;
                                                                                                                                       echo "  between_pos_list_str1:".$between_pos_list_str1;
                                                                                                                                       echo "  l_pos_id_str:".$l_pos_id_str;
                                                                                                                                       echo "  between_pos_list_str:".$between_pos_list_str;  echo " q_pos_id_str:".$q_pos_id_str; echo "<br/>between_pos_list_str2:".$between_pos_list_str2;
                                                                                                                                       echo " between_pos_list_str2:".$between_pos_list_str2;
                                                                                                                                       echo " e_pos_id_str——str:".$e_pos_id_str;   echo "   between_pos_strt_3:".$between_pos_list_str3;
                                                                                                                                       echo "   g_pos_id:".$g_pos_id;echo "<br/>between_pos_list_str4:".$between_pos_list_str4." p_pos_str: ".$p_pos_id."p_one_str: ".$p_one_str ;
                                                                                                                                 */      //
                                                                                                                                  //  echo "<br/>   pos_str 811:".$pos_str."<br/> e_pos_id: ".$e_pos_id."p_pos_id: ".$p_pos_id ;
                                                                                                                                 }
                                                                                                                             }
                                                                                                                         }else{
                                                                                                                              //  echo "<br/>".$g_pos_id."没有更大的多图图层<br/> e_pos_id: ".$e_pos_id;

                                                                                                                             $pos_str=TaozhuangVrPos::get_pos_str_g($tzid,$order_pos_list_str,$x_pos_id,$l_pos_id,$q_pos_id,$e_pos_id,$g_pos_id,$g_pos_pos);
                                                                                                                              $pos_array[]=$pos_str;

                                                                                                                          //  echo "<br/>   pos_str 822:".$pos_str."<br/>";
                                                                                                                         }


                                                                                                                    }
                                                                                                                }
                                                                                                            }

                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }else{
                                                                                                $one_pos_str_list=TaozhuangVrPos::get_one_pos($tzid,$q_pos_pos);
                                                                                                $between_pos_list_str=TaozhuangVrPos::get_between_pos_str($tzid, $l_pos_id, $q_pos_id);
                                                                                                $between_pos_list_str1=TaozhuangVrPos::get_between_pos_str($tzid,$x_pos_id,$l_pos_id);
                                                                                                $pos_str=$order_pos_list_str.$x_pos_id.",".$between_pos_list_str1.$l_pos_id.",".$between_pos_list_str.$q_pos_id.$one_pos_str_list;
                                                                                                // echo "<br/> order_pos_list_str:".$order_pos_list_str." x_pos_id: ".$x_pos_id." l_pos_id:".$l_pos_id." between_pos_list_str:".$between_pos_list_str." q_pos_id:".$q_pos_id." one_pos_str_list:".$one_pos_str_list;

                                                                                                // echo "<br/> pos_str 9 : ".$pos_str;
                                                                                                $pos_array[]=$pos_str;

                                                                                            }

                                                                                        }
                                                                                     }else{
                                                                                         $one_pos_str_list=TaozhuangVrPos::get_one_pos($tzid,$q_pos_pos);
                                                                                         $between_pos_list_str=TaozhuangVrPos::get_between_pos_str($tzid, $l_pos_id, $q_pos_id);
                                                                                         $between_pos_list_str1=TaozhuangVrPos::get_between_pos_str($tzid,$x_pos_id,$l_pos_id);
                                                                                         $pos_str=$order_pos_list_str.$x_pos_id.",".$between_pos_list_str1.$l_pos_id.",".$between_pos_list_str.$q_pos_id.$one_pos_str_list;
                                                                                        // echo "<br/> order_pos_list_str:".$order_pos_list_str." x_pos_id: ".$x_pos_id." l_pos_id:".$l_pos_id." between_pos_list_str:".$between_pos_list_str." q_pos_id:".$q_pos_id." one_pos_str_list:".$one_pos_str_list;

                                                                                      //    echo "<br/> pos_strs  zz: ".$pos_str."<br/> order:".$q_pos_order;
                                                                                         $pos_array[]=$pos_str;

                                                                                       //  echo " <br/>  pos_str 10:".$pos_str;
                                                                                     }

                                                                                 }

                                                                            }
                                                                        }
                                                                    }

                                                                }else{
                                                                    
                                                                    if($l_pos_pos!=$multiple_pos){
                                                                        $one_pos_str_list=TaozhuangVrPos::get_one_pos($tzid,$l_pos_id);
                                                                        $pos_str=$order_pos_list_str.$x_pos_id.",".$l_pos_id.$one_pos_str_list;
                                                                        $pos_array[]=$pos_str;
                                                                          //  echo " <br/>  pos_str 11 : ".$pos_str;
                                                                    }
                                                                }
                                                                
                                                            }
                                                        }
                                                        $key = array_search($x_pos_id, $pos_find_array);
                                                        if ($key !== false)
                                                            array_splice($pos_find_array, $key, 1);
                                                         }

                                            }

                                        }
                                    }
                                }
                            }
                        }
                   // }
                }else{
                    $result=0;
                    $sql="select id from taozhuang_vr_pos where tzid=:tzid order by pos asc ";
                    $vr_pos_list=TaozhuangVrPos::findBySql($sql,array(":tzid"=>$tzid))->asArray()->all();
                    $vr_id_str="";
                    for($w=0;$w<count($vr_pos_list);$w++){
                         $vr_pos_id=$vr_pos_list[$w]["id"];
                         if($w==0){
                             $vr_id_str.=$vr_pos_id;
                         }else{
                             $vr_id_str.=",".$vr_pos_id;
                         }
                     }
                     if(count($vr_pos_list)>0||$vr_id_str!=""){
                         $pos_array[]=$vr_id_str;
                         $pos_array=array_unique($pos_array);
                     }
                    
                }

            echo "<br/>count:".count($pos_array)."<br/>";
            print_r($pos_array);
             /*   for($aa=0;$aa<count($pos_array);$aa++){
                    print_r($pos_array[$aa]);echo "<br/>";
                }*/
              exit();
                //pos_list  img_list(对应的img_id )     tzid=$tzid   img_id=0  img_b=0 img_d=0 img_f=0  img_l=0 img_r=0 img_u=0
                $sql="SELECT id FROM taozhuang_vr_buffer WHERE tzid=:tzid";
                $buffer_list=TaozhuangVrBuffer::findBySql($sql,array(":tzid"=>$tzid))->asArray()->all();
                if(count($buffer_list)==0){
                    for($b=0;$b<count($pos_array);$b++){
                        $pos_list=$pos_array[$b];
                        $pos_array_temp=explode(",", $pos_list);
                        $img_id_str="";
                        if(is_array($pos_array_temp)){
                            for($c=0;$c<count($pos_array_temp);$c++){
                                $c_vr_pos_id=$pos_array_temp[$c];
                                $vr_pos_info=TaozhuangVrPos::findOne($c_vr_pos_id);
                                if($vr_pos_info){
                                    if($img_id_str==""){
                                        $img_id_str.=$vr_pos_info->img_id;
                                    }else{
                                        $img_id_str.=",".$vr_pos_info->img_id;
                                    }
                                }
                            }
                        }
                       
                        $vr_pos_buffer=new TaozhuangVrBuffer();
                        $vr_pos_buffer->tzid=$tzid;
                        $vr_pos_buffer->pos_list=$pos_list;
                        $vr_pos_buffer->img_list=$img_id_str;
                        $vr_pos_buffer->img_id=0;
                        $vr_pos_buffer->img_b=0;
                        $vr_pos_buffer->img_d=0;
                        $vr_pos_buffer->img_f=0;
                        $vr_pos_buffer->img_l=0;
                        $vr_pos_buffer->img_r=0;
                        $vr_pos_buffer->img_u=0;
                        $result=$vr_pos_buffer->save();
                      
                    }
                    echo $result;exit();
                }
              
                
             //tzid==0    
            }else{
                echo 2;exit();
            }

 
    }

    /**
     * buffer生成大图
     */
    public function actionCreateBigImage(){
        
         
    }
}

 